package absc.wizard;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class SWOTWizardPage extends WizardPage implements KeyListener{
	
	private IWorkspaceRoot workspaceRoot;
	private String containerName;
	private Text txtNomeArquivo, txtCaminhoArquivo;

	protected SWOTWizardPage() {
		super("SWOT Analysis");
		setTitle("Create SWOT Analysis");
		setDescription("Create a new SWOT analysis");
		workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
	}
	
	public void init(IStructuredSelection selection) {
		IContainer container = null;
		if (selection != null && !selection.isEmpty()) {
			Object object = selection.getFirstElement();
			if (IFile.class.isInstance(object) && !IContainer.class.isInstance(object)) {
				container = ((IFile)object).getParent();
			} else if (IContainer.class.isInstance(object)) {
				container = (IContainer)object;
			}
		}
		initContainerName(container);
	}

	private void initContainerName(IContainer elem) {
		containerName = (elem == null) ? "" : elem.getFullPath().makeRelative().toString(); 
	}

	public void createControl(Composite parent) {
		GridLayout layoutTela;		
		Composite tela;
		GridData layoutLabel, layoutEdit;
		CLabel lblNomeArquivo, lblCaminhoArquivo;		
		String pathArquivo;
		
		pathArquivo = workspaceRoot.getLocation().toOSString() + "\\" + containerName + "\\swot\\";
		
		initializeDialogUnits(parent);
		
		tela = new Composite(parent, SWT.NONE);
		
		layoutTela = new GridLayout();
		layoutTela.numColumns = 2;
		
		layoutLabel = new GridData();
		layoutLabel.horizontalAlignment = SWT.RIGHT;
		layoutLabel.widthHint = convertWidthInCharsToPixels(20);
		
		layoutEdit = new GridData();
		layoutEdit.heightHint = -1;
		layoutEdit.widthHint = convertWidthInCharsToPixels(50);
		
		lblNomeArquivo = new CLabel(tela, SWT.NONE);		
		lblNomeArquivo.setText("Name:");
		lblNomeArquivo.setLayoutData(layoutLabel);
		
		txtNomeArquivo = new Text(tela, SWT.BORDER);
		txtNomeArquivo.setOrientation(33554432);
		txtNomeArquivo.setLayoutData(layoutEdit);
		txtNomeArquivo.setEditable(true);
		txtNomeArquivo.addKeyListener(this);
		
		
		lblCaminhoArquivo = new CLabel(tela, SWT.NONE);
		lblCaminhoArquivo.setText("File's Path:");
		lblCaminhoArquivo.setLayoutData(layoutLabel);
		
		txtCaminhoArquivo = new Text(tela, SWT.BORDER);
		txtCaminhoArquivo.setOrientation(33554432);
		txtCaminhoArquivo.setLayoutData(layoutEdit);
		txtCaminhoArquivo.setText(pathArquivo);
		txtCaminhoArquivo.setEditable(false);
		
		tela.setLayout(layoutTela);
		
		setControl(tela);
		Dialog.applyDialogFont(tela);
		setPageComplete(false);
	}

	public void keyPressed(KeyEvent arg0) {
	}

	public void keyReleased(KeyEvent arg0) {
		String pathArquivo;		
		
		pathArquivo = workspaceRoot.getLocation().toOSString() + "\\" + containerName + "\\swot\\";		
		pathArquivo += this.txtNomeArquivo.getText() + ".swo";
		
		this.txtCaminhoArquivo.setText(pathArquivo);
		
		if (this.txtCaminhoArquivo.getText().equalsIgnoreCase("")){
			setErrorMessage("SWOT name is empty");
			setPageComplete(false);
		}
		else{			
			IPath path = new Path(pathArquivo);
			if (path.toFile().exists()) {
				setErrorMessage("A file with this name already exists");
				setPageComplete(false);				
			}
			else{
				setErrorMessage(null);
				setPageComplete(true);
			}			
		}		
	}

	public IFolder getSWOTFolder() {		
		IPath path = new Path(containerName).append("\\swot\\");
		return workspaceRoot.getFolder(path);
	}

	public String getSWOTName() {		
		return this.txtNomeArquivo.getText();
	}
}
